public class QuadraticEquation {
    private double a;
    private double b;
    private double c;

    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getDiscriminant() {
        return b * b - 4 * a * c;
    }

    public boolean hasRealRoots() {
        return getDiscriminant() >= 0;
    }

    public Complex[] solve() {
        double discriminant = getDiscriminant();
        Complex root1, root2;

        if (discriminant >= 0) {
            // 实数根
            root1 = new Complex((-b + Math.sqrt(discriminant)) / (2 * a), 0);
            root2 = new Complex((-b - Math.sqrt(discriminant)) / (2 * a), 0);
        } else {
            // 复数根
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            root1 = new Complex(realPart, imaginaryPart);
            root2 = new Complex(realPart, -imaginaryPart);
        }

        return new Complex[]{root1, root2};
    }

    public static void main(String[] args) {
        QuadraticEquation eq = new QuadraticEquation(1, -3, 2);
        Complex[] roots = eq.solve();
        System.out.println("方程的根:");
        for (Complex root : roots) {
            System.out.println(root);
        }
    }
}

class Complex {
    private double real;
    private double imaginary;

    public Complex(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    @Override
    public String toString() {
        if (imaginary == 0) {
            return String.valueOf(real);
        } else if (real == 0) {
            return imaginary + "i";
        } else if (imaginary < 0) {
            return real + " - " + (-imaginary) + "i";
        } else {
            return real + " + " + imaginary + "i";
        }
    }
}
